package org.nbile.commons;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.apache.commons.codec.binary.Base64;
import org.stellar.sdk.responses.SubmitTransactionResponse;
import org.stellar.sdk.xdr.TransactionResult;
import org.stellar.sdk.xdr.TransactionResultCode;
import org.stellar.sdk.xdr.XdrDataInputStream;

/**
 * Document Start 
 * 返回码处理工具类
 * Document End 
 * Author: songwenpeng@le.com
 * Time: 2016年10月6日 下午9:29:47
 */
public class ResultUtils {

	//未知错误
	public static int UNKNOW_ERROR = -1000;
	/**
	 * Document Start 
	 * 判断Transaction是否通过了SCP共识确认
	 * 通过了返回True
	 * 否则返回False
	 * Document End 
	 * Author: songwenpeng@le.com
	 * Time: 2016年10月6日 下午9:31:40
	 * @param resultXdr
	 * @return
	 */
	public static boolean isOKTransactionBySCP(String resultXdr) {
		Base64 base64Codec = new Base64();
		byte[] bytes = base64Codec.decode(resultXdr);
		TransactionResult result;
		try {
			result = TransactionResult.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
			if (result.getResult().getDiscriminant() == TransactionResultCode.txSUCCESS) {
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Document Start 
	 * 返回结果字符串
 	* txSUCCESS = 0, // all operations succeeded
	* txFAILED = -1, // one of the operations failed (none were applied)
	* txTOO_EARLY = -2,         // ledger closeTime before minTime
	* txTOO_LATE = -3,          // ledger closeTime after maxTime
	* txMISSING_OPERATION = -4, // no operation was specified
	* txBAD_SEQ = -5,           // sequence number does not match source account
	* txBAD_AUTH = -6,             // too few valid signatures / wrong network
	* txINSUFFICIENT_BALANCE = -7, // fee would bring account below reserve
	* txNO_ACCOUNT = -8,           // source account not found
	* txINSUFFICIENT_FEE = -9,     // fee is too small
	* txBAD_AUTH_EXTRA = -10,      // unused signatures attached to transaction
	* txINTERNAL_ERROR = -11       // an unknown error occured
	* -1000 异常时返回
	 * Document End 
	 * Author: songwenpeng@le.com
	 * Time: 2016年12月13日 下午9:44:51
	 * @param response
	 * @return
	 */
	public static int getResultCode(SubmitTransactionResponse response) {
		//如果返回结果为空 则返回未知异常
		if (response == null) {
			return ResultUtils.UNKNOW_ERROR;
		}

		//非空返回其他异常
		Base64 base64Codec = new Base64();
		byte[] bytes = base64Codec.decode(response.getResultXdr());
		TransactionResult result;
		try {
			result = TransactionResult.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
			return result.getResult().getDiscriminant().getValue();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResultUtils.UNKNOW_ERROR;
		}
	}

	/**
	 * 判断一个Transaction是否成功
	 * @param response
	 * @return
	 */
	public static boolean isOKTransaction(SubmitTransactionResponse response) {
		Base64 base64Codec = new Base64();
		byte[] bytes = base64Codec.decode(response.getResultXdr());
		TransactionResult result;
		try {
			result = TransactionResult.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
			if (result.getResult().getDiscriminant().getValue() == 0)
				return true;
			else
				return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
}
